package com.yun.algorithmproblem.leetcode;

public class Leetcode2873 {

	public long maximumTripletValue(int[] nums) {
		long maxRes=0;
		int[] leftMax=new int[nums.length];
		int[] rightMax=new int[nums.length];
		int[] leftMin=new int[nums.length];
		int[] rightMin=new int[nums.length];
		int max=0,min=Integer.MAX_VALUE;
		for (int i = 0; i < nums.length; i++) {
			leftMax[i]=max;
			leftMin[i]=min;
			max=Math.max(max,nums[i]);
			min=Math.min(min,nums[i]);
		}
		max=0;
		min=Integer.MAX_VALUE;
		for (int i = nums.length-1; i >=0 ; i--) {
			rightMax[i]=max;
			rightMin[i]=min;
			max=Math.max(max,nums[i]);
			min=Math.min(min,nums[i]);
			if(i!=nums.length-1 && i!=0){
				maxRes=Math.max(maxRes, (long) (leftMax[i] - nums[i]) *rightMax[i]);
				maxRes=Math.max(maxRes, (long) (leftMin[i] - nums[i]) *rightMin[i]);
			}
		}
		return maxRes;
	}
}
